package com.hexinfo.dmpro.sparing.dto;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false,of = {"sourceDbName","sourceTableName","sourceQueryCriteria","targetDbName","targetTableName","targetQueryCriteria"})
public class WhereScriptDTO {

    /**
     * 库名
     */
    @ApiModelProperty(value="库名")
    private String sourceDbName;

    /**
     * 表名
     */
    @ApiModelProperty(value="表名")
    private String sourceTableName;

    /**
     * 条件sql
     */
    @ApiModelProperty(value="条件sql")
    private String sourceQueryCriteria;

    //目标库
    private String targetDbName;

    //目标表名
    private String targetTableName;

    private String targetQueryCriteria;

    //源库.表
    public String getDbTableName(){
        return sourceDbName + "." + sourceTableName;
    }

    public String getTableFlag() {
        return sourceDbName + "." + sourceTableName + " " + getSourceQueryCriteria();
    }

    public String getSourceQueryCriteria() {
        if (StrUtil.isBlank(sourceQueryCriteria)) {
            return "";
        }
        String prefix = sourceQueryCriteria.trim().substring(0, 5);
        if ("where".equals(prefix.toLowerCase())) {
            return sourceQueryCriteria;
        }
        return "where " + sourceQueryCriteria;
    }
}
